﻿#include "titlebar.h"
#include "ui_titlebar.h"
#include "commonutils.h"
#include <QMouseEvent>

TitleBar::TitleBar(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TitleBar)
{
    ui->setupUi(this);
    init();
}

TitleBar::~TitleBar()
{
    delete ui;
}

void TitleBar::setTitleIcon(const QString &filePath)
{
    QPixmap pix(filePath);
    ui->iconLabel->setFixedSize(pix.size());

    ui->iconLabel->setPixmap(pix);
}

void TitleBar::setTitleContent(const QString &titleContent)
{
    ui->titleContent->setText(titleContent);
}

void TitleBar::saveRestoreInfo(const QPoint &point, const QSize &size)
{
    restorePos=point;
    restoreSize=size;
}

void TitleBar::getRestoreInfo(QPoint &point, QSize &size)
{
    point=restorePos;
    size=restoreSize;
}

bool TitleBar::windowIsMax()
{
    if(!ui->buttonMax->isVisible())
    {
        return true;
    }
    return false;
}

void TitleBar::mousePressEvent(QMouseEvent *event)
{
    if(!windowIsMax())
    {
        moveAble=true;
        startMovePos=event->globalPos();
        event->accept();
    }
}

void TitleBar::mouseMoveEvent(QMouseEvent *event)
{
    if (moveAble)
    {
        QPoint movePoint = event->globalPos() - startMovePos;     //鼠标移动的全局点坐标
        QPoint windowPoint = parentWidget()->parentWidget()->pos();               //父窗体坐标
        QPoint windowMovePoint = windowPoint + movePoint;         //父窗体需要移动的坐标
        parentWidget()->parentWidget()->move(windowMovePoint);

        startMovePos = event->globalPos();          //更新开始移动点坐标

        event->accept();
    }
}

void TitleBar::mouseReleaseEvent(QMouseEvent *event)
{
    moveAble = false;       //移动结束

    event->accept();
}

void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (ui->buttonMax->isVisible())        //双击时，最大化按钮可见则最大化
    {
        onButtonMaxClicked();
    }
    else        //还原按钮可见则还原
    {
        onButtonRestoreClicked();
    }
    return QWidget::mouseDoubleClickEvent(event);
}

void TitleBar::paintEvent(QPaintEvent *event)
{
    if (width() != parentWidget()->width())
    {
        setFixedWidth(parentWidget()->width());
    }
}

void TitleBar::onButtonRestoreClicked()
{
    ui->buttonRestore->setVisible(false);
    ui->buttonMax->setVisible(true);

    emit buttonRestoreClicked();
}

void TitleBar::onButtonMaxClicked()
{
    ui->buttonMax->setVisible(false);
    ui->buttonRestore->setVisible(true);

    emit buttonMaxClicked();
}

void TitleBar::init()
{
    CommonUtils::loadStyleSheet(this,"Title","QWidget#TitleBar \
                                {background-color: rgba(255, 0, 0, 100);}");
    moveAble=ui->buttonMax->isVisible();
    initConnect();
}


void TitleBar::initConnect()
{
    connect(ui->buttonMin, &QPushButton::clicked, this, &TitleBar::buttonMinClicked);
    connect(ui->buttonRestore, &QPushButton::clicked, this, &TitleBar::onButtonRestoreClicked);
    connect(ui->buttonMax, &QPushButton::clicked, this, &TitleBar::onButtonMaxClicked);
    connect(ui->buttonClose, &QPushButton::clicked, this, &TitleBar::buttonCloseClicked);
}
